/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.ps.server;

import com.ps.server.classes.languages;
import java.net.MalformedURLException;
import java.util.Hashtable;
import java.util.Vector;
import java.util.logging.Level;
import java.util.logging.Logger;
import org.wiztools.commons.Implementation;
import org.wiztools.restclient.*;

/**
 *
 * @author Ricardo
 */
public class ClientRest extends org.wiztools.restclient.RESTTestCase {

    private String usr = "4798MHJNN0B69DDVRV5A7ZZ8O6RQZYWI";
    private String pwd = "";
    private String url = "http://localhost/1.5.x/api/";
    private Vector getVector;
    private String resource;
    private Hashtable getHashtable;

    public Vector getVector() {
        return getVector;
    }

    public Hashtable getconfig() {
        return this.getHashtable;
    }

    public void ejecutarMetodoPut(String resource, String contenidoEnviar, String tipoContenido, String charSet) {
        try {
            RequestBean requestBean = new RequestBean();
            requestBean.setUrl(new java.net.URL(url.concat(resource)));
            requestBean.setMethod(HTTPMethod.PUT);
            requestBean.setAuthPreemptive(true);
            requestBean.setAuthUsername(this.usr);
            requestBean.setAuthPassword(pwd.toCharArray());
            ReqEntityBean body = new ReqEntityBean(contenidoEnviar, tipoContenido, charSet);
            requestBean.addAuthMethod(HTTPAuthMethod.BASIC);
            requestBean.setBody(body);
            Request requestClient = requestBean;

            RequestExecuter executer = Implementation.of(RequestExecuter.class);
            //System.out.println(requestBean.toString());
            //System.out.println("------------------------------------------------------");
            // 'request' and 'view' are the objects created in the previous sections:
            executer.execute(requestClient, view);
        } catch (MalformedURLException ex) {
            Logger.getLogger(ClientRest.class.getName()).log(Level.SEVERE, null, ex);
        }

    }

    /**
     * Parte descriptiva. Que puede consistir de varias frases o párrafos.
     *
     * @etiqueta texto específico de la etiqueta
     */
    public void ejecutarMetodoPost(String resource, String contenidoEnviar, String tipoContenido, String charSet) {
        try {
            RequestBean requestBean = new RequestBean();
            requestBean.setUrl(new java.net.URL(url.concat(resource)));
            requestBean.setMethod(HTTPMethod.POST);
            requestBean.setAuthPreemptive(true);
            requestBean.setAuthUsername(this.usr);
            requestBean.setAuthPassword(pwd.toCharArray());
            ReqEntityBean body = new ReqEntityBean(contenidoEnviar, tipoContenido, charSet);
            requestBean.addAuthMethod(HTTPAuthMethod.BASIC);
            requestBean.setBody(body);
            Request requestClient = requestBean;

            RequestExecuter executer = Implementation.of(RequestExecuter.class);
            //System.out.println(requestBean.toString());
            //System.out.println("------------------------------------------------------");
            // 'request' and 'view' are the objects created in the previous sections:
            executer.execute(requestClient, view);
        } catch (MalformedURLException ex) {
            Logger.getLogger(ClientRest.class.getName()).log(Level.SEVERE, null, ex);
        }

    }

    /**
     * Variable argumento= <ul> <li>"?display=full" <li>"/1"; </ul> resource =
     * <ul> <li>"languages" <li>"zones"; <li>"addresses"; <li>"categories";
     * <li>"suppliers"; </ul> ejecutarMetodoGet("languages",argumento);
     *
     * @param
     */
    public void ejecutarMetodoGet(String resource, String argumento) {
        try {
            this.resource = resource;
            RequestBean requestBean = new RequestBean();
            requestBean.setUrl(new java.net.URL(url.concat(resource).concat(argumento)));
            requestBean.setMethod(HTTPMethod.GET);
            requestBean.setAuthPreemptive(true);
            requestBean.setAuthUsername(this.usr);
            requestBean.setAuthPassword(pwd.toCharArray());
            requestBean.addAuthMethod(HTTPAuthMethod.BASIC);
            Request requestClient = requestBean;
            RequestExecuter executer = Implementation.of(RequestExecuter.class);
            //System.out.println(requestBean.toString());
            //System.out.println("------------------------------------------------------");
            // 'request' and 'view' are the objects created in the previous sections:
            executer.execute(requestClient, view);
        } catch (MalformedURLException ex) {
            Logger.getLogger(ClientRest.class.getName()).log(Level.SEVERE, null, ex);
        }
    }
    View view = new View() {

        @Override
        public void doStart(Request request) {
            //System.out.println(request.getBody());
            // do nothing!
        }

        @Override
        public void doResponse(Response response) {
            Resource rs = new Resource();
            if (resource.equals("manufacturers")) {
                rs.getResourceManufacturers(response.getResponseBody());
            }
            if (resource.equals("languages")) {
                rs.getResource(response.getResponseBody());
            }
            if (resource.equals("suppliers")) {
                rs.getResourceSuppliers(response.getResponseBody());
            }
            if (resource.equals("categories")) {
                rs.getResourceCategories(response.getResponseBody());
            }
            if (resource.equals("combinations")) {
                rs.getResourceCombinations(response.getResponseBody());
            }
            if (resource.equals("product_features")) {
                rs.getResourceProduct_features(response.getResponseBody());
            }
            if (resource.equals("products")) {
                rs.getResourceProducts(response.getResponseBody());
            }
            if (resource.equals("configurations")) {
                rs.getResourceConfigurations(response.getResponseBody());
                getHashtable = rs.GetConfig();
            }
            if (resource.equals("taxes")) {
                rs.getResourceTaxes(response.getResponseBody());
                getHashtable = rs.GetConfig();
            }

            if (resource.equals("tax_rules")) {
                rs.getResourceTax_rules(response.getResponseBody());
                getHashtable = rs.GetConfig();
            }
            
                        if (resource.equals("tax_rule_groups")) {
                rs.getResourceTax_rule_groups(response.getResponseBody());
                getHashtable = rs.GetConfig();
            }

            getVector = rs.GetColeccion();
            System.out.println(response.getResponseBody());
        }

        @Override
        public void doCancelled() {
            //System.out.println(response.getResponseBody());
            // do nothing!
        }

        @Override
        public void doEnd() {
            //System.out.println(response.getResponseBody());
            // do nothing!
        }

        @Override
        public void doError(final String error) {
            System.err.println(error);
        }
    };

    languages dato() {
        languages lang = new languages();
        lang.setActive("0");
        lang.setDate_format_full("m/j/Y H:i:s");
        lang.setDate_format_lite("m/j/Y");
        lang.setId("1");
        lang.setIs_rtl("0");
        lang.setIso_code("en");
        lang.setLanguage_code("en-us");
        lang.setName("English usalon");
        return lang;
    }
}
